"""
Watermark messages
"""
#
# message.py
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

from itertools import cycle

from numpy import fix
from numpy import fromstring
from numpy import uint8
from numpy import unpackbits
from numpy import zeros

def bitarrray(m):
    """Convert an input string to array of bits.

    Keyword arguments:
    m -- the input string to convert

    """
    message_bytes = fromstring(m, dtype=uint8)
    w = unpackbits(message_bytes, axis=-1)
    return w

def watermark_message(w, frame_size, signal_wide):
    m = zeros(signal_wide, dtype=int)
    circ = cycle(w)
    num_frames = int(fix(signal_wide / frame_size))
    # loop: work within the splitted signal in blocks
    for frame in range(num_frames):
        start = frame * frame_size
        end = start + frame_size
        m[start:end] = circ.next()
    start = end
    # last samples
    if (num_frames * frame_size) != signal_wide:
        m[start:] = circ.next()
    return m
